﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace RunVistaGadget31
{
    class FilePathDeterminer
    {
        private String FolderDialogDescription;
        private String FileName;
        private String PreviousFilePath;
        private FileOpenDialogType FODT;

        public FilePathDeterminer(string folderDialogDescription, string defaultFileName, string prevFilePath, FileOpenDialogType fodt)
        {
            FolderDialogDescription = folderDialogDescription;
            FileName = defaultFileName;
            PreviousFilePath = prevFilePath;
            FODT = fodt;
        }

        public string GetFilePath()
        {
            string FilePath = GetSelectedPath();

            if (!String.IsNullOrEmpty(FilePath))
            {
                string PrevFileName = TryGetPreviousFileName();
                if (!String.IsNullOrEmpty(PrevFileName))
                {
                    FileName = PrevFileName;
                }
                if (FODT == FileOpenDialogType.FolderBrowserDialog)
                {
                    if (Path.GetPathRoot(FilePath) != FilePath)
                    {
                        FilePath += System.IO.Path.DirectorySeparatorChar;
                    }
                    FilePath += FileName;
                }
            }

            return FilePath;
        }

        private string TryGetPreviousFileName()
        {
            string PreviousGadgetName = string.Empty;
            if (!String.IsNullOrEmpty(PreviousFilePath))
            {
                PreviousGadgetName = System.IO.Path.GetFileName(PreviousFilePath);
            }

            return PreviousGadgetName;
        }

        private string GetSelectedPath()
        {
            string SelectedPath = String.Empty;
            bool ThisDialogResult = false;
            if (FODT == FileOpenDialogType.FolderBrowserDialog)
            {
                FolderBrowserDialog Dialog = new FolderBrowserDialog();
                Dialog.ShowNewFolderButton = true;
                Dialog.Description = FolderDialogDescription;

                ThisDialogResult = (Dialog.ShowDialog() == DialogResult.OK);
                SelectedPath = Dialog.SelectedPath;
            }
            else if (FODT == FileOpenDialogType.OpenFileDialog)
            {
                OpenFileDialog Dialog = new OpenFileDialog();
                Dialog.Title = FolderDialogDescription;
                Dialog.Filter = FileName + "(" + FileName + ")|" + FileName + "|All files (*.*)|*.*";

                ThisDialogResult = (Dialog.ShowDialog() == DialogResult.OK);
                SelectedPath = Dialog.FileName;
            }
            return
                    (ThisDialogResult == true) ? SelectedPath : string.Empty;
        }

        public enum FileOpenDialogType
        {
            FolderBrowserDialog,
            OpenFileDialog
        }
    }
}
